package com.qb.hotelTV.Utils;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.qb.hotelTV.constants.AppConstants;

/**
 * TV焦点管理器
 * 优化TV端焦点处理和动画效果
 */
public class TvFocusManager {
    
    private static final long FOCUS_ANIMATION_DURATION = 200; // 动画持续时间
    private static final float FOCUS_SCALE = AppConstants.TV_FOCUS_SCALE; // 焦点缩放比例
    
    /**
     * 为View设置焦点监听器
     */
    public static void setupFocusListener(View view) {
        setupFocusListener(view, FOCUS_SCALE);
    }
    
    /**
     * 为View设置焦点监听器（自定义缩放比例）
     */
    public static void setupFocusListener(View view, float scale) {
        if (view == null) return;
        
        view.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                animateViewScale(v, scale);
                // 可以添加焦点获取时的其他效果
                onFocusGained(v);
            } else {
                animateViewScale(v, 1.0f);
                // 可以添加焦点失去时的其他效果
                onFocusLost(v);
            }
        });
        
        // 设置可聚焦
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
    }
    
    /**
     * 执行View缩放动画
     */
    private static void animateViewScale(View view, float scale) {
        if (view == null) return;
        
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", view.getScaleX(), scale);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", view.getScaleY(), scale);
        
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(scaleX, scaleY);
        animatorSet.setDuration(FOCUS_ANIMATION_DURATION);
        animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
        
        animatorSet.start();
    }
    
    /**
     * 焦点获得时的处理
     */
    private static void onFocusGained(View view) {
        // 可以添加阴影、边框等效果
        view.setElevation(8f);
        
        // 确保View在父容器中可见
        view.bringToFront();
    }
    
    /**
     * 焦点失去时的处理
     */
    private static void onFocusLost(View view) {
        // 移除阴影等效果
        view.setElevation(0f);
    }
    
    /**
     * 强制请求焦点
     */
    public static boolean requestFocus(View view) {
        if (view != null && view.isFocusable()) {
            return view.requestFocus();
        }
        return false;
    }
    
    /**
     * 清除焦点
     */
    public static void clearFocus(View view) {
        if (view != null && view.hasFocus()) {
            view.clearFocus();
        }
    }
    
    /**
     * 设置下一个焦点
     */
    public static void setNextFocusView(View current, View next, int direction) {
        if (current == null || next == null) return;
        
        switch (direction) {
            case View.FOCUS_LEFT:
                current.setNextFocusLeftId(next.getId());
                break;
            case View.FOCUS_RIGHT:
                current.setNextFocusRightId(next.getId());
                break;
            case View.FOCUS_UP:
                current.setNextFocusUpId(next.getId());
                break;
            case View.FOCUS_DOWN:
                current.setNextFocusDownId(next.getId());
                break;
        }
    }
    
    /**
     * 创建焦点链（水平）
     */
    public static void createHorizontalFocusChain(View... views) {
        if (views == null || views.length < 2) return;
        
        for (int i = 0; i < views.length; i++) {
            View current = views[i];
            setupFocusListener(current);
            
            // 设置左边的焦点
            if (i > 0) {
                current.setNextFocusLeftId(views[i - 1].getId());
            }
            
            // 设置右边的焦点
            if (i < views.length - 1) {
                current.setNextFocusRightId(views[i + 1].getId());
            }
        }
    }
    
    /**
     * 创建焦点链（垂直）
     */
    public static void createVerticalFocusChain(View... views) {
        if (views == null || views.length < 2) return;
        
        for (int i = 0; i < views.length; i++) {
            View current = views[i];
            setupFocusListener(current);
            
            // 设置上边的焦点
            if (i > 0) {
                current.setNextFocusUpId(views[i - 1].getId());
            }
            
            // 设置下边的焦点
            if (i < views.length - 1) {
                current.setNextFocusDownId(views[i + 1].getId());
            }
        }
    }
    
    /**
     * 创建网格焦点系统
     */
    public static void createGridFocus(View[][] grid) {
        if (grid == null || grid.length == 0) return;
        
        int rows = grid.length;
        int cols = grid[0].length;
        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                View current = grid[i][j];
                if (current == null) continue;
                
                setupFocusListener(current);
                
                // 设置上下左右的焦点
                if (i > 0 && grid[i - 1][j] != null) { // 上
                    current.setNextFocusUpId(grid[i - 1][j].getId());
                }
                if (i < rows - 1 && grid[i + 1][j] != null) { // 下
                    current.setNextFocusDownId(grid[i + 1][j].getId());
                }
                if (j > 0 && grid[i][j - 1] != null) { // 左
                    current.setNextFocusLeftId(grid[i][j - 1].getId());
                }
                if (j < cols - 1 && grid[i][j + 1] != null) { // 右
                    current.setNextFocusRightId(grid[i][j + 1].getId());
                }
            }
        }
    }
    
    /**
     * 高亮显示可聚焦的View（调试用）
     */
    public static void highlightFocusableViews(View rootView, boolean highlight) {
        if (rootView == null) return;
        
        if (rootView.isFocusable()) {
            if (highlight) {
                rootView.setBackgroundColor(0x33FF0000); // 半透明红色
            } else {
                rootView.setBackground(null);
            }
        }
        
        // 递归处理子View
        if (rootView instanceof android.view.ViewGroup) {
            android.view.ViewGroup viewGroup = (android.view.ViewGroup) rootView;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                highlightFocusableViews(viewGroup.getChildAt(i), highlight);
            }
        }
    }
} 